<template>
    <app-section>
        <van-nav-bar
                left-arrow
                left-text="我"
                @click-left="backRoute">
           <!-- <van-icon name="scan" slot="right" size="1.5em"/> @click-right="scanClick"-->
        </van-nav-bar>
        <van-pull-refresh v-model="pullLoading" @refresh="onLoad">
            <van-list
                    v-model="loading"
                    :finished="finished"
                    :error.sync="error"
                    error-text="请求失败，点击重新加载"
                    @load="onLoad"
            >

                    <van-cell-group class="content-margin head">
                        <van-cell
                             v-for="(item, index) in perList"
                             :key="index"
                        >
                            <template #title>
                                <span style="color: #666">{{ getSubStringText(item.title,11) }}</span>
                                <van-icon style="padding-left: 10px;font-size: 16px;" name="certificate" color="#07c160"></van-icon>
                            </template>
                            <van-image
                                slot="right-icon"
                                class="img"
                                round
                                width="3rem"
                                height="3rem"
                                :src="personalList.avatar"/>
                        </van-cell>

                         <van-cell class="fontColor" :title="personalList.username" :label="personalList.post">
                            <template #right-icon >
                                <van-icon  style="padding-top: 1rem" name="manager-o" color="orange"></van-icon>
                            </template>
                         </van-cell>

                        <div class="van-hairline--bottom"></div>
                        <van-cell class="fontColor" is-link :title="businessCard" @click="toPersonalDetails"/>

                    </van-cell-group>
                    <van-cell-group class="content-cell">
                        <van-cell class="cell" is-link value="收藏">
                            <template #icon >
                                <van-icon class="icon-style"  name="like-o" color="orange"></van-icon>
                            </template>
                        </van-cell>
                    <div class="van-hairline--bottom"></div>
                    <van-cell class="cell" is-link  title="红包" icon="bill-o" color="lightred">
                        <template #icon >
                            <van-icon class="icon-style"  name="bill-o" color="pink"></van-icon>
                        </template>
                    </van-cell>
                </van-cell-group>

                    <van-cell-group class="content-cell">
                        <van-cell class="cell" is-link  value="设置" @click="toPersonalDetails">
                            <template #icon >
                                <van-icon class="icon-style"  name="setting-o" color="#999"></van-icon>
                            </template>
                        </van-cell>
                    </van-cell-group>

                    <van-cell-group class="content-cell">
                        <van-button  block @click="handleLogout" style="color: red;">
                        退出登录
                    </van-button>
                    </van-cell-group>

            </van-list>
        </van-pull-refresh>
        <!--扫码-->
        <!--<app-scan ref="scanRef" @callback="callback"></app-scan>-->
    </app-section>

</template>

<script>
    import {Dialog } from 'vant'
    import { getAction,postAction,getFileAccessHttpUrl } from '@/api'
    import AppSection from '@/components/AppSection'
    import { Toast } from 'vant';
    export default {
        name: "personalHome",
        components:{AppSection},
        data(){
          return{
              businessCard:'我的资料',
              perList:[],
              personalList:{
                  avatar:'',
                  realname:'',
                  username:'',
                  post:''
              },
              error:false,
              loading: false,
              pullLoading: false,
              finished:false,
              positionUrl:'/sys/position/list',
              departUrl:'/sys/user/userDepartList',
              userUrl:'/sys/user/queryById',
              userId:'',
              id:''
          }
        },
        methods:{
            backRoute(){
                this.$router.push({name: 'home'})
            },
            getSubStringText(text,len){
                if(!text || text.length==0){
                    return ''
                }
                if(text.length<len){
                    return text;
                }
                return text.substr(0,len)+"..."
            },
            onLoad(){
                this.loading = true
                this.pullLoading = true
                getAction(this.departUrl,{userId:this.$store.getters.userid}).then(res=> {
                    this.loading = false
                    this.pullLoading = false
                    console.log("用户1",res)
                    if (res.success) {
                        this.perList=[];
                        if(res.result&&res.result.length >0){
                            for (let item of res.result){
                                this.perList.push(item)
                                console.log('0909',item)
                                break;

                            }
                        }
                    }
                    this.finished = true
                }).catch(e=>{
                    console.log("请求错误",e)
                    this.error = true;
                })
                // this.loading = true
                this.pullLoading = true
                getAction(this.userUrl,{id:this.$store.getters.userid}).then(res=> {
                    this.loading = false
                    this.pullLoading = false
                    console.log("用户",res)
                    if (res.success) {
                        let perArr = res.result
                        if (perArr.avatar && perArr.avatar.length > 0)
                        this.personalList.avatar = getFileAccessHttpUrl(perArr.avatar)
                        this.personalList.realname = perArr.realname
                        this.personalList.username = perArr.username
                        this.personalList.post = perArr.post
                    }
                    this.finished = true
                }).catch(e=>{
                    console.log("请求错误",e)
                    this.error = true;
                })
                this.pullLoading = true
                getAction(this.positionUrl).then(res=> {
                    this.pullLoading = false
                    this.loading = false
                    console.log("用户1",res)
                    if (res.success) {
                        let postArr = res.result.records
                        for (let item of postArr ){
                            if (this.personalList.post == item.code){
                                this.personalList.post = item.name
                            }
                        }
                    }
                    this.finished = true
                }).catch(e=>{
                    console.log("请求错误",e)
                    this.error = true;
                })
            },
            toPersonalDetails(){
                // let params = {id:this.$store.getters.userid}
                // console.log('xx',params),params:params
                this.$router.push({name: 'personalDetails'})
            },
            handleLogout() {
                const self = this
                Dialog.confirm({
                    title: '提示',
                    message:"您确定要退出吗?"
                }).then(() => {
                        self.$router.push({path: '/logout'})
                    }).catch(() => {
                    })

            },
            scanClick(){
                this.$refs.scanRef.scanClick()
            },
            callback(result){
               this.mettingSign(result);
            },
            mettingSign(mettingId){
                postAction("/metting/eoaMettingSign/mettingSign",{mettingId:mettingId,userId:this.$store.getters.userid}).then(res=> {
                    console.log("会议签到mettingSign",res)
                    if(res.success){
                        Toast.success('签到成功');
                    }else{
                        Toast.fail('签到成功');
                    }
                })
            }
        }
    }
</script>

<style scoped lang="less">
    .content-margin{margin: 0.8rem;background-color: white;}
    .font{
        color: #222;
        font-size: 18px;
        font-weight: bold;
        padding-left: 1rem;
    }
    .content-cell{margin-top: 2rem}
    .fontColor{color: #aaaaaa;font-size: 15px}
    .icon-style{margin:auto 0.8rem;font-weight: 900;font-size: 16px}
    .cell{font-weight: bold}
</style>